package org.skyui.beans;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import org.skyui.annotations.SkyField;
import org.skyui.annotations.SkyManyToMany;
import org.skyui.annotations.SkyTable;
import org.skyui.beanmanagers.BeanRelationManager;
import org.skyui.beanmanagers.BeanSetManager;
import org.skyui.beans.AbstractBean;
import org.skyui.validators.Validators;

import com.baycrypt.server.beans.networks.WLANBean;
import com.baycrypt.server.beans.people.UserBean;

@Entity
@SkyTable(itemsCaption = "News", selectedItemCaption = "Manage News", addItemCaption = "Add News")
public class NewsBean extends AbstractBean implements Serializable {

	public final static String PRIORITY_HIGH = "HIGH";
	public final static String PRIORITY_NORMAL = "NORMAL";

	String userID;
	
	String targetBeanId;

	Class targetBeanClass;

	String moduleName;

	String eventType;

	String action;

	String priority;

	/*
	 * 
	 * @SkyField(caption = "IP Address", read = "getIPAddress", write =
	 * "setIPAddress", validator = Validators.IPAddress.class) String ipAddress;
	 * 
	 * @SkyField(caption = "Type", read = "getType", write = "setType") String
	 * type;
	 */

	public NewsBean() {
	}

	public NewsBean(UserBean user, String targetBeanId, Class targetBeanClass,
			String moduleName, String eventType, String action,
			String priority, OrganizationBean organization) {
		this.setOrganization(organization);
		this.userID = user.getId();
		this.targetBeanId = targetBeanId;
		this.targetBeanClass = targetBeanClass;
		this.moduleName = moduleName;
		this.eventType = eventType;
		this.action = action;
		this.priority = priority;
	}

	private boolean isSameDay(Date d1, Date d2) {
		return (d1.getDate() == d2.getDate() && d1.getMonth() == d2.getMonth() && d1
				.getYear() == d2.getYear());
	}

	private boolean isPreviousDay(Date d1, Date d2) {
		return (d1.getDate() - 1 == d2.getDate()
				&& d1.getMonth() == d2.getMonth() && d1.getYear() == d2
				.getYear());
	}

	private String humanReadableTime(Date d) {
		Date today = new Date();
		String timeString;
		if (isSameDay(today, d))
			timeString = " today at "
					+ d.getHours()
					% 12
					+ ":"
					+ (d.getMinutes() > 9 ? d.getMinutes() : "0" + d
							.getMinutes())
					+ (d.getHours() < 12 ? " am" : " pm");
		else if (isPreviousDay(today, d))
			timeString = " yesterday at "
					+ d.getHours()
					% 12
					+ ":"
					+ (d.getMinutes() > 9 ? d.getMinutes() : "0" + d
							.getMinutes())
					+ (d.getHours() < 12 ? " am" : " pm");
		else
			timeString = " on " + d.toLocaleString();
		return timeString;
	}

	public String toString() {
		
		UserBean user = (UserBean) BeanSetManager.getManager(UserBean.class, this.getOrganization()).getBeanById(userID);


		
		String firstName, lastName;

		if (user == null) {
			firstName = "deleted";
			lastName = "";
		} else {
			firstName = user.getFirstName();
			lastName = user.getLastName();
		}
		
		String role = "<font color=\"grey\">"
				+ (user.isAdmin() ? "(Admin) " : "(User)") + "</font> ";
		return "<b><font color=\"#0000A0\"> " + firstName + " "
				+ lastName + "</font></b><br/> "
				+ "<font color=\"grey\">" + action
				+ humanReadableTime(this.getCreationTime()) + " .</font>";
	}

}